package com.feshfans.bridge;

import com.feshfans.controller.special.MainController;
import com.feshfans.controller.RegisteredController;
import javafx.application.Platform;
import org.jivesoftware.smack.packet.Presence;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.FullJid;
import org.jxmpp.jid.Jid;

public class RosterBridge extends AbstractBridge {

    public static RosterBridge bridge = new RosterBridge();

    public static RosterBridge getInstance(){
        return bridge;
    }

    private RegisteredController controller;

    private RosterBridge(){}



    public synchronized void processSubscribe(Jid from, Presence subscribeRequest){

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                MainController mainController = (MainController)controller;
                mainController.processSubscribe(from.toString());
            }
        });

    }
    public synchronized void handlerPresence(FullJid address, Presence presence){

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                BareJid jid = address.asBareJid();
                MainController mainController = (MainController)controller;
                mainController.handlerPresence(jid,presence);
            }
        });
    }

    @Override
    public synchronized void register(RegisteredController controller) {
        this.controller = controller;
    }

    @Override
    public synchronized void unregister(RegisteredController controller) {
        this.controller = null;
    }
}
